import undetected_chromedriver as uc
from lxml import etree
from selenium.webdriver.common.by import By
from dbse.mysql.db_mysql_insert import addThsReportInform
from dbse.mysql.db_entity_stock_ths_report_inform import StockThsReportInform
from selenium.webdriver import ChromeOptions
from date.dateUtil import isToday
import time
import pymysql
from file.readFile import read
import datetime

def sel_report_page(browser,db,sql,page):
    endTag = False
    content = browser.page_source
    html = etree.fromstring(content,etree.HTMLParser())
    result = html.xpath('//tbody//tr')      # tbody 下的每一行数据
    for index, tr in enumerate(result):
        tds = tr.getchildren()
        print(f"====index:{index}")
        print(tds[1].find('a').text)    # 股票代码
        print(tds[2].find('a').text)    # 股票名称
        print(tds[3].text)    #公告日期
        print(tds[4].text)  #营业收入
        print(tds[5].text)  # 同比增长
        print(tds[6].text)  # 季度环比增长
        print(tds[7].text)  # 净利润
        print(tds[8].text)  # 同比增长
        print(tds[9].text)  # 季度环比增长
        print(tds[10].text) # 每股收益
        print(tds[11].text) # 每股净资产
        print(tds[12].text) # 净资产收益率
        print(tds[13].text) # 每股经营现金流量
        print(tds[14].text) # 销售毛利率
        entity = StockThsReportInform(tds[1].find('a').text,
                                      tds[2].find('a').text,
                                      tds[3].text,
                                      tds[4].text,
                                      tds[5].text,
                                      tds[6].text,
                                      tds[7].text,
                                      tds[8].text,
                                      tds[9].text,
                                      tds[10].text,
                                      tds[11].text,
                                      tds[12].text,
                                      tds[13].text,
                                      tds[14].text)
        # 判断是否是当天的消息
        if isToday(tds[3].text):
            addThsReportInform(db,sql,entity)
        else:
            endTag = True;
            break

    return endTag


today = datetime.date.today()
driver_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
option = ChromeOptions()
option.add_argument('--headless')
# browser = uc.Chrome(driver_executable_path=driver_path,options=option)
browser = uc.Chrome(driver_executable_path=driver_path)
browser.set_window_size(1366,768)
browser.get('https://data.10jqka.com.cn/financial/yjyg')
print('====begin====')

selContinue = True      # 是否继续抓取
page = 1                # 当前抓取的页码
db = pymysql.connect(host='39.98.82.109',user='root',password='landleaf@123.com',port=3306,db='stock')
sql = 'INSERT INTO stock_ths_report_inform (stock_code, stock_name,inform_date,yysr,yysrtbzz,yysrjdhbzz,jlr,jlrtbzz,jlrjdhbzz,mgsy,mgjzc,jzcsyl,mgjyxjll,xsmll) VALUES (%s, %s,%s,%s,%s, %s,%s,%s,%s, %s,%s,%s,%s, %s)'

while selContinue:
    endTag = sel_report_page(browser,db,sql,page)
    page = page +1
    # 触发下一页
    nextPage = browser.find_element(By.XPATH,f"//a[@page='{page}']")
    nextPage.click()
    time.sleep(2)  #等待页面加载
    # 跳出条件
    if endTag:
        selContinue = False

